﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;

using FieldLib.DataAccess;

namespace FieldDevice.UI
{
    public partial class DeviceHistoryReadsControl : UserControl
    {
        public DeviceHistoryReadsControl()
        {
            InitializeComponent();
        }

        public Device Device;

        private void HistoryQueryButton_Click(object sender, EventArgs e)
        {
            try
            {
                Query();
            }
            catch (Exception exp)
            {
                MessageBox.Show(exp.ToString());
            }
        }
        void Query()
        {
            bool useBegin = this.checkBoxFrom.Checked;
            bool useEnd = this.checkBoxTo.Checked;
            DateTime begintime = new DateTime(this.fromDate.Value.Year, this.fromDate.Value.Month, this.fromDate.Value.Day,
                this.fromTime.Value.Hour, this.fromTime.Value.Minute, this.fromTime.Value.Second);
            DateTime endtime = new DateTime(this.toDate.Value.Year, this.toDate.Value.Month, this.toDate.Value.Day,
                this.toTime.Value.Hour, this.toTime.Value.Minute, this.toTime.Value.Second);
            DataTable table = DeviceManager.GlobalDeviceManager.FieldReadLog.LoadReads(
                (int)this.Device.NCPAddress, this.Device.DeviceModel,
                null, //所有类型的Reads
                useBegin, begintime, useEnd, endtime,
                -1);
            this.ReadListView.Items.Clear();
            if (table.Rows.Count == 0)
                return;
            string[] valuenames = null;
            switch(this.Device.Category)
            {
                case Device.DeviceCategory.PowerMeter:
                case Device.DeviceCategory.HighVoltageDevice:
                    valuenames = new string[]{ "VA", "VB", "VC", "IA", "IB", "IC", "P", "Q", "PF" };
                    break;
                default:
                    return;
            }
            
            while (this.ReadListView.Columns.Count < (valuenames.Length + 1))
                this.ReadListView.Columns.Add(new ColumnHeader());
            this.ReadListView.Columns[0].Text = "时间";

            int[] typeids = new int[valuenames.Length];
            for(int i=0;i<valuenames.Length;i++)
            {
                string name = valuenames[i];
                this.ReadListView.Columns[i+1].Text = name;
                this.ReadListView.Columns[i].TextAlign = HorizontalAlignment.Right;
                typeids[i] = DeviceManager.GlobalDeviceManager.FieldReadLog.GetReadTypeId(this.Device.DeviceModel, name);
            }
            DateTime currenttime = (DateTime)table.Rows[0][2];
            float[] values = new float[valuenames.Length];

            foreach (DataRow row in table.Rows)
            {
                DateTime readtime = (DateTime)row[2];
                long readid = (long)row[3];
                int readtypeid = (int)row[0];
                //ReadType readtype = DeviceManager.GlobalDeviceManager.FieldReadLog.GetReadType(readtypeid);
                int i = 0; //i为纪录在纪录组中的顺序号
                for (; i < valuenames.Length; i++)
                {
                    if (readtypeid == typeids[i])
                        break;//找到
                }
                if (i == valuenames.Length)
                    continue;//不是需要显示的量
                TimeSpan span = readtime - currenttime;
                if (span.TotalMilliseconds > 1000)
                {//上一组纪录结束,
                    ShowReads(valuenames, values, currenttime);
                    values = new float[valuenames.Length];//新一组纪录
                    currenttime = readtime;
                    values[i] = (float)(double)row[1];//首个记录
                }
                else
                    values[i] = (float)(double)row[1];


                //string typetext = null;
                //if (readtype == null)
                //    typetext = "unknow read type " + readtypeid;
                //else
                //    typetext = readtype.ReadName;
                //ListViewItem item = this.ReadListView.Items.Add(typetext);//row[0].ToString());
                //item.SubItems.Add(readtype.ReadName);
                //item.SubItems.Add(row[1].ToString());
                //item.SubItems.Add(readtime.ToString());
            }
        }

        private void ShowReads(string[] valuenames, float[] values, DateTime currenttime)
        {
            ListViewItem item = this.ReadListView.Items.Add(currenttime.ToString());//row[0].ToString());
            foreach (float value in values)
                item.SubItems.Add(value.ToString());
            //item.SubItems.Add(readtype.ReadName);
            //item.SubItems.Add(row[1].ToString());
            //item.SubItems.Add(readtime.ToString());
        }

        private void DeviceHistoryReadsControl_Load(object sender, EventArgs e)
        {
            this.toDate.Value = DateTime.Now;
            this.fromDate.Value = DateTime.Now - new TimeSpan(24, 0, 0);
        }
    }
}
